package top.sdaily.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.sdaily.core.mybatis.Conditions;
import top.sdaily.core.service.BaseServiceImpl;
import top.sdaily.core.utils.IdGeneratorUtils;
import top.sdaily.mapper.RightMapper;
import top.sdaily.web.exception.FailedException;
import top.sdaily.web.model.Right;
import top.sdaily.web.service.RightService;

import java.util.List;

/**
 * Created by soya on 2016/10/29.
 */
@Service
@Transactional
public class RightServiceImpl extends BaseServiceImpl<Right> implements RightService {

    @Autowired
    RightMapper rightMapper;

    @Override
    public List<Right> getAll() {
        return rightMapper.findList(condition());
    }

    @Override
    public Right create(Right right) {
        Right code = rightMapper.findOne(condition().eq("code", right.getCode()));
        if(code != null) {
            throw new FailedException("代码[{0}]已存在",right.getCode());
        }
        right.setPkid(IdGeneratorUtils.fresh());
        rightMapper.insert(right);
        return right;
    }

    @Override
    public void modify(Right right) {
        rightMapper.update(right);
    }
}
